<template>
  <div class="tool-website-traffic">
    <h2 class="bar-title mb-6 break-words">
      <span class="text-gray-1000 text-26 font-semibold break-words w-full">{{$t('detail_websiteTraffic')}}</span>
    </h2>
    <div class="flex items-stretch justify-around flex-wrap gap-x-5 gap-y-4 border border-gray-2000 rounded-md px-5 py-5">
      <div class="flex-shrink-0">
        <div class="text-lg text-gray-1000 text-center">{{$t('detail_lastMonthSVisits')}}</div>
        <div class="flex items-center justify-center mt-3">
          <span class="text-xl text-gray-1000 font-semibold">{{ hasRealVal(traffic.last_1_month_count) ? $vReset(traffic.last_1_month_count) : '--'}}</span>
          <svg-icon className="w-5 h-5 ml-3 mr-2"
            :iconName='hasRealVal(traffic.last_1_month_growth) && traffic.last_1_month_growth > 0 ? "up" : "down"'
            v-if="hasRealVal(traffic.last_1_month_growth) && traffic.last_1_month_growth!== 0"></svg-icon>
          <span class="text-xl font-semibold"
            :class="{'text-red-1000':hasRealVal(traffic.last_1_month_growth) && traffic.last_1_month_growth <0,'text-green-1100':hasRealVal(traffic.last_1_month_growth) && traffic.last_1_month_growth >0}"
            v-if="hasRealVal(traffic.last_1_month_growth) && traffic.last_1_month_growth!== 0">({{$vReset(traffic.last_1_month_growth)}})</span>
        </div>
      </div>
      <div class="flex-shrink-0">
        <div class="text-lg text-gray-1000 text-center">{{$t('detail_last3MmonthSVisits')}}</div>
        <div class="flex items-center justify-center mt-3">
          <span class="text-xl text-gray-1000 font-semibold">{{hasRealVal(traffic.last_3_month_count) ? $vReset(traffic.last_3_month_count) : '--'}}</span>
          <svg-icon className="w-5 h-5 ml-3 mr-2"
            :iconName='hasRealVal(traffic.last_3_month_growth) && traffic.last_3_month_growth > 0 ? "up" : "down"'
            v-if="hasRealVal(traffic.last_3_month_growth) && traffic.last_3_month_growth!== 0"></svg-icon>
          <span class="text-xl font-semibold"
            :class="{'text-red-1000':hasRealVal(traffic.last_3_month_growth) && traffic.last_3_month_growth <0,'text-green-1100':hasRealVal(traffic.last_3_month_growth) && traffic.last_3_month_growth >0}"
            v-if="hasRealVal(traffic.last_3_month_growth) && traffic.last_3_month_growth!== 0">({{$vReset(traffic.last_3_month_growth)}})</span>
        </div>
      </div>
      <div class="flex-shrink-0">
        <div class="text-lg text-gray-1000 text-center">{{$t('detail_last6MonthSVisits')}}</div>
        <div class="flex items-center justify-center mt-3">
          <span class="text-xl text-gray-1000 font-semibold">
            {{hasRealVal(traffic.last_6_month_count) ? (traffic.last_6_month_count === 0 ? '--' : $vReset(traffic.last_6_month_count)) : '--'}}
          </span>
          <svg-icon className="w-5 h-5 ml-3 mr-2"
            :iconName='hasRealVal(traffic.last_6_month_growth) && traffic.last_6_month_growth > 0 ? "up" : "down"'
            v-if="hasRealVal(traffic.last_6_month_growth) && traffic.last_6_month_growth!== 0"></svg-icon>
          <span class="text-xl font-semibold"
            :class="{'text-red-1000':hasRealVal(traffic.last_6_month_growth) && traffic.last_6_month_growth <0,'text-green-1100':hasRealVal(traffic.last_6_month_growth) && traffic.last_6_month_growth >0}"
            v-if="hasRealVal(traffic.last_6_month_growth) && traffic.last_6_month_growth!== 0">({{$vReset(traffic.last_6_month_growth)}})</span>
        </div>
      </div>
    </div>

    <div class="mt-8 grid gap-5 md:grid-cols-2"
      v-if="(hasRealVal(totalVisitorsList) || hasRealVal(visitorsGrowthList)) && (totalVisitorsList.length > 0 || visitorsGrowthList > 0)">
      <div class="chart-bar">
        <div class="text-lg text-gray-1000 font-bold">{{$t('detail_totalVisitors')}}</div>
        <template v-if="hasRealVal(totalVisitorsList) && totalVisitorsList.length > 0">
          <no-ssr>
            <line-chart :chartData="totalVisitorsList"
              dId="totalVisitors"></line-chart>
          </no-ssr>
        </template>
        <el-empty v-else
          :description="$t('vs_noData')"></el-empty>
      </div>
      <div class="chart-bar">
        <div class="text-lg text-gray-1000 font-bold">{{$t('detail_visitorsGrowth')}}</div>
        <template v-if="hasRealVal(visitorsGrowthList) && visitorsGrowthList.length > 0">
          <no-ssr>
            <line-chart :chartData="visitorsGrowthList"
              dId="visitorsGrowth"></line-chart>
          </no-ssr>
        </template>
        <el-empty v-else
          :description="$t('detail_noData')"></el-empty>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ToolDetailWebsiteTraffic',
  props: {
    traffic: {
      type: Object | null,
      default() {
        return {}
      },
    },
  },
  watch: {
    traffic: {
      handler: function (val) {
        if (val) {
          this.totalVisitorsList = this.traffic.estimated_monthly_visits_list
          this.visitorsGrowthList = this.traffic.visits_growth_list
        }
      },
      immediate: true,
      deep: true,
    },
  },
  data() {
    return {
      totalVisitorsList: [],
      visitorsGrowthList: [],
    }
  },
}
</script>
<style lang='scss' scoped>
.tool-website-traffic {
  svg {
    color: rgba(0, 0, 0, 0) !important;
  }
}
</style>